

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.Custom &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.ops.Assign" href="mindspore.ops.Assign.html" />
    <link rel="prev" title="mindspore.ops.SparseTensorDenseMatmul" href="mindspore.ops.SparseTensorDenseMatmul.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.ops.html">mindspore.ops</a> &raquo;</li>
        
      <li>mindspore.ops.Custom</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/ops/mindspore.ops.Custom.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-ops-custom">
<h1>mindspore.ops.Custom<a class="headerlink" href="#mindspore-ops-custom" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.ops.Custom">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.ops.</code><code class="sig-name descname">Custom</code><span class="sig-paren">(</span><em class="sig-param">func</em>, <em class="sig-param">out_shape</em>, <em class="sig-param">out_dtype</em>, <em class="sig-param">func_type</em>, <em class="sig-param">bprop=None</em>, <em class="sig-param">reg_info=None</em><span class="sig-paren">)</span><a class="reference internal" href="../../_modules/mindspore/ops/operations/custom_ops.html#Custom"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#mindspore.ops.Custom" title="Permalink to this definition">¶</a></dt>
<dd><p><cite>Custom</cite> primitive is used for user defined operators and is to enhance the expressive ability of built-in
primitives. You can construct a <cite>Custom</cite> object with a predefined function, which describes the computation
logic of a user defined operator. You can also construct another <cite>Custom</cite> object with another predefined
function if needed. Then these <cite>Custom</cite> objects can be directly used in neural networks.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This is an experimental prototype that is subject to change.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>func</strong> (<em>Union</em><em>[</em><em>function</em><em>, </em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a><em>]</em>) – <ul>
<li><p>function: If func is of function type, then func should be a Python function which describes the
computation logic of a user defined operator. The function can be one of the following:</p>
<ol class="arabic simple">
<li><p>A AKG operator implementation function, which can use ir builder/tvm compute/hybrid grammar.</p></li>
<li><p>A TBE operator implementation function.</p></li>
<li><p>A pure python function</p></li>
</ol>
</li>
<li><p>str: If func is of str type, then str should be a path of binary file along with a function name.
This could only be used when func_type is “aot”. Currently “aot” supports GPU/CPU(linux only) platform.
“aot” means ahead of time, in which case Custom directly launches user defined “xxx.so” file as an
operator. Users need to compile a handwriting “xxx.cu”/”xxx.cc” file into “xxx.so” ahead of time,
and offer the path of the file along with a function name.</p>
<ul>
<li><p>”xxx.so” file generation:</p>
<p>1) GPU Platform: Given user defined “xxx.cu” file (ex. “{path}/add.cu”), use nvcc command to compile
it.(ex. “nvcc –shared -Xcompiler -fPIC -o add.so add.cu”)</p>
<p>2) CPU Platform: Given user defined “xxx.cc” file (ex. “{path}/add.cc”), use g++/gcc command to compile
it.(ex. “g++ –shared -fPIC  -o add.so add.cc”)</p>
</li>
<li><p>Define a “xxx.cc”/”xxx.cu” file:</p>
<p>”aot” is a cross-platform identity. The functions defined in “xxx.cc” or “xxx.cu” share the same args.
Typically, the function should be as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span> <span class="n">func</span><span class="p">(</span><span class="nb">int</span> <span class="n">nparam</span><span class="p">,</span> <span class="n">void</span> <span class="o">**</span><span class="n">params</span><span class="p">,</span> <span class="nb">int</span> <span class="o">*</span><span class="n">ndims</span><span class="p">,</span> <span class="n">int64_t</span> <span class="o">**</span><span class="n">shapes</span><span class="p">,</span> <span class="n">const</span> <span class="n">char</span> <span class="o">**</span><span class="n">dtypes</span><span class="p">,</span>
         <span class="n">void</span> <span class="o">*</span><span class="n">stream</span><span class="p">,</span> <span class="n">void</span> <span class="o">*</span><span class="n">extra</span><span class="p">)</span>
</pre></div>
</div>
<p>Parameters:</p>
<ul class="simple">
<li><p>nparam(int): total number of inputs plus outputs; suppose the operator has 2 inputs and 3 outputs,
then nparam=5</p></li>
<li><p>params(void **): a pointer to the array of inputs and outputs’ pointer; the pointer type of inputs
and outputs is void * ; suppose the operator has 2 inputs and 3 outputs, then the first input’s
pointer is params[0] and the second output’s pointer is params[3]</p></li>
<li><p>ndims(int *): a pointer to the array of inputs and outputs’ dimension num; suppose params[i] is a
1024x1024 tensor and params[j] is a 77x83x4 tensor, then ndims[i]=2, ndims[j]=3.</p></li>
<li><p>shapes(int64_t **): a pointer to the array of inputs and outputs’ shapes(int64_t *); the ith
input’s jth dimension’s size is shapes[i][j](0&lt;=j&lt;ndims[i]); suppose params[i] is a 2x3 tensor and
params[j] is a 3x3x4 tensor, then shapes[i][0]=2, shapes[j][2]=4.</p></li>
<li><p>dtypes(const char **): a pointer to the array of inputs and outputs’ types(const char *);
(ex. “float32”, “float16”, “float”, “float64”, “int”, “int8”, “int16”, “int32”, “int64”, “uint”,
“uint8”, “uint16”, “uint32”, “uint64”, “bool”)</p></li>
<li><p>stream(void *): stream pointer, only used in cuda file</p></li>
<li><p>extra(void *): used for further extension</p></li>
</ul>
<p>Return Value(int):</p>
<ul class="simple">
<li><p>0: raise no Exception</p></li>
<li><p>larger than 0: will raise Exception</p></li>
</ul>
<p>Examples: see details tests/st/ops/graph_kernel/custom/aot_test_files/</p>
</li>
<li><p>Use it in Custom:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Custom</span><span class="p">(</span><span class="n">func</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{path}</span><span class="s2">/</span><span class="si">{file_name}</span><span class="s2">:</span><span class="si">{func_name}</span><span class="s2">&quot;</span><span class="p">,</span><span class="o">...</span><span class="p">)</span>
<span class="p">(</span><span class="n">ex</span><span class="o">.</span> <span class="n">Custom</span><span class="p">(</span><span class="n">func</span><span class="o">=</span><span class="s2">&quot;./reorganize.so:CustomReorganize&quot;</span><span class="p">,</span> <span class="n">out_shape</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">out_dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
</pre></div>
</div>
</li>
</ul>
</li>
</ul>
</p></li>
<li><p><strong>out_shape</strong> (<em>Union</em><em>[</em><em>function</em><em>, </em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#list" title="(in Python v3.8)"><em>list</em></a><em>, </em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#tuple" title="(in Python v3.8)"><em>tuple</em></a><em>]</em>) – <p>The output shape infer function or the value of output shape of
<cite>func</cite>.</p>
<p>If func has single output, then the value of output shape is a list or tuple of int.</p>
<p>If func has multiple outputs, then the value of output shape is a tuple, each item represents the shape
of each output.</p>
</p></li>
<li><p><strong>out_dtype</strong> (Union[function, <a class="reference internal" href="../mindspore/mindspore.dtype.html#mindspore.dtype" title="mindspore.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.dtype</span></code></a>, tuple[<a class="reference internal" href="../mindspore/mindspore.dtype.html#mindspore.dtype" title="mindspore.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.dtype</span></code></a>]]) – <p>The output data type
infer function or the value of output data type of <cite>func</cite>.</p>
<p>If func has single output, then the value of output shape is a <cite>mindspore.dtype</cite>.</p>
<p>If func has multiple outputs, then the value of output shape is a tuple of <cite>mindspore.dtype</cite>, each item
represents the data type of each output.</p>
</p></li>
<li><p><strong>func_type</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a>) – <p>The implementation type of <cite>func</cite>, should be one of [“akg”, “tbe”, “aot”, “pyfunc”]. Each
<cite>func_type</cite> only supports specific platforms(targets). The supported platforms of <cite>func_type</cite>:</p>
<ul>
<li><p>”akg”: supports [“Ascend”, “GPU”].</p></li>
<li><p>”tbe”: supports [“Ascend”].</p></li>
<li><p>”aot”: supports [“GPU”, “CPU”].</p></li>
<li><p>”pyfunc”: supports [“CPU”].</p></li>
</ul>
</p></li>
<li><p><strong>bprop</strong> (<em>function</em>) – The back propagation function of <cite>func</cite>. Default: None.</p></li>
<li><p><strong>reg_info</strong> (<em>Union</em><em>[</em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a><em>, </em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#dict" title="(in Python v3.8)"><em>dict</em></a><em>, </em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#list" title="(in Python v3.8)"><em>list</em></a><em>, </em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#tuple" title="(in Python v3.8)"><em>tuple</em></a><em>]</em>) – <p>Represents the registration information(reg info) of <cite>func</cite> with
json format of type str or dict. The reg info specifies supported data types and formats of inputs and
outputs, attributes and target of <cite>func</cite>. Default: None.</p>
<p>If reg info is a list or tuple, then each item should be with json format of type str or dict, which
represents the registration information of <cite>func</cite> in a specific target. You need to invoke <cite>CustomRegOp</cite>
or the subclass of <cite>RegOp</cite> to generate the reg info for <cite>func</cite>. Then you can invoke
<cite>custom_info_register</cite> to bind the reg info to <cite>func</cite> or just pass the reg info to <cite>reg_info</cite> parameter.
The <cite>reg_info</cite> parameter takes higher priority than <cite>custom_info_register</cite> and the reg info in a
specific target will be registered only once.</p>
<p>If reg info is not set, then we will infer the data types and formats from the inputs of <cite>Custom</cite> operator.</p>
<p>Please note that, if <cite>func_type</cite> is “tbe” or the <cite>func</cite> only supports some specified data types and formats,
or it has attribute inputs, then you should set the reg info for <cite>func</cite>.</p>
</p></li>
</ul>
</dd>
</dl>
<dl class="simple">
<dt>Inputs:</dt><dd><ul class="simple">
<li><p><strong>input</strong> (Union(tuple, list)) - The input tuple or list is made up of multiple tensors, and attributes
value(optional).</p></li>
</ul>
</dd>
<dt>Outputs:</dt><dd><p>Tensor or tuple[Tensor], execution results.</p>
</dd>
</dl>
<dl class="field-list simple">
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><ul class="simple">
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If the type of <cite>func</cite> is invalid or the type of register information for <cite>func</cite> is invalid.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If <cite>func_type</cite> is invalid.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If the register information is invalid, including the target is not supported, the input numbers
    or the attributes of <cite>func</cite> differs in different targets.</p></li>
</ul>
</dd>
</dl>
<dl class="simple">
<dt>Supported Platforms:</dt><dd><p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.ops</span> <span class="k">as</span> <span class="nn">ops</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">CustomRegOp</span><span class="p">,</span> <span class="n">custom_info_register</span><span class="p">,</span> <span class="n">DataType</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.nn</span> <span class="kn">import</span> <span class="n">Cell</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Example, func_type = &quot;akg&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">outer_product</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">c</span> <span class="o">=</span> <span class="n">output_tensor</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">i0</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
<span class="gp">... </span>        <span class="k">for</span> <span class="n">i1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
<span class="gp">... </span>            <span class="n">c</span><span class="p">[</span><span class="n">i0</span><span class="p">,</span> <span class="n">i1</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="gp">... </span>            <span class="k">for</span> <span class="n">i2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
<span class="gp">... </span>                <span class="n">c</span><span class="p">[</span><span class="n">i0</span><span class="p">,</span> <span class="n">i1</span><span class="p">]</span> <span class="o">=</span> <span class="n">c</span><span class="p">[</span><span class="n">i0</span><span class="p">,</span> <span class="n">i1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">i0</span><span class="p">,</span> <span class="n">i2</span><span class="p">]</span> <span class="o">*</span> <span class="n">b</span><span class="p">[</span><span class="n">i2</span><span class="p">,</span> <span class="n">i1</span><span class="p">])</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">c</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">AkgNet</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">AkgNet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="k">def</span> <span class="nf">infer_func</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="n">x</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">program</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">Custom</span><span class="p">(</span><span class="n">outer_product</span><span class="p">,</span> <span class="n">out_shape</span><span class="o">=</span><span class="n">infer_func</span><span class="p">,</span> <span class="n">out_dtype</span><span class="o">=</span><span class="n">infer_func</span><span class="p">,</span> \
<span class="gp">... </span>                                  <span class="n">func_type</span><span class="o">=</span><span class="s2">&quot;akg&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">program</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Example, func_type = &quot;tbe&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">square_with_bias_op_info</span> <span class="o">=</span> <span class="n">CustomRegOp</span><span class="p">()</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">fusion_type</span><span class="p">(</span><span class="s2">&quot;OPAQUE&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">attr</span><span class="p">(</span><span class="s2">&quot;bias&quot;</span><span class="p">,</span> <span class="s2">&quot;required&quot;</span><span class="p">,</span> <span class="s2">&quot;float&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">input</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;x&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">dtype_format</span><span class="p">(</span><span class="n">DataType</span><span class="o">.</span><span class="n">F32_Default</span><span class="p">,</span> <span class="n">DataType</span><span class="o">.</span><span class="n">F32_Default</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">dtype_format</span><span class="p">(</span><span class="n">DataType</span><span class="o">.</span><span class="n">F16_Default</span><span class="p">,</span> <span class="n">DataType</span><span class="o">.</span><span class="n">F16_Default</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">target</span><span class="p">(</span><span class="s2">&quot;Ascend&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">get_op_info</span><span class="p">()</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@custom_info_register</span><span class="p">(</span><span class="n">square_with_bias_op_info</span><span class="p">)</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">square_with_bias</span><span class="p">(</span><span class="n">input_x</span><span class="p">,</span> <span class="n">output_y</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">kernel_name</span><span class="o">=</span><span class="s2">&quot;square_with_bias&quot;</span><span class="p">):</span>
<span class="gp">... </span>    <span class="kn">import</span> <span class="nn">te.lang.cce</span>
<span class="gp">... </span>    <span class="kn">from</span> <span class="nn">te</span> <span class="kn">import</span> <span class="n">tvm</span>
<span class="gp">... </span>    <span class="kn">from</span> <span class="nn">topi.cce</span> <span class="kn">import</span> <span class="n">util</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="n">shape</span> <span class="o">=</span> <span class="n">input_x</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;shape&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">dtype</span> <span class="o">=</span> <span class="n">input_x</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;dtype&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="n">shape</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">shape_refine</span><span class="p">(</span><span class="n">shape</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">data</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;data&quot;</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">with</span> <span class="n">tvm</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">cce</span><span class="p">():</span>
<span class="gp">... </span>        <span class="n">res0</span> <span class="o">=</span> <span class="n">te</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">cce</span><span class="o">.</span><span class="n">vmul</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="gp">... </span>        <span class="n">res</span> <span class="o">=</span> <span class="n">te</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">cce</span><span class="o">.</span><span class="n">vadds</span><span class="p">(</span><span class="n">res0</span><span class="p">,</span> <span class="n">bias</span><span class="p">)</span>
<span class="gp">... </span>        <span class="n">sch</span> <span class="o">=</span> <span class="n">te</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">cce</span><span class="o">.</span><span class="n">auto_schedule</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="n">config</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;print_ir&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
<span class="gp">... </span>              <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">kernel_name</span><span class="p">,</span>
<span class="gp">... </span>              <span class="s2">&quot;tensor_list&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">data</span><span class="p">,</span> <span class="n">res</span><span class="p">]}</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="n">te</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">cce</span><span class="o">.</span><span class="n">cce_build_code</span><span class="p">(</span><span class="n">sch</span><span class="p">,</span> <span class="n">config</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">TbeNet</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">TbeNet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">square_with_bias</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">Custom</span><span class="p">(</span><span class="n">square_with_bias</span><span class="p">,</span> <span class="n">out_shape</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">_</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span> \
<span class="gp">... </span>                                           <span class="n">out_dtype</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">_</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span> <span class="n">func_type</span><span class="o">=</span><span class="s2">&quot;tbe&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">square_with_bias</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">res</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Example, func_type = &quot;aicpu&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">resize_bilinear_op_info</span> <span class="o">=</span> <span class="n">CustomRegOp</span><span class="p">(</span><span class="s2">&quot;ResizeBilinear&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">fusion_type</span><span class="p">(</span><span class="s2">&quot;OPAQUE&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">input</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;input&quot;</span><span class="p">,</span> <span class="s2">&quot;required&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;output&quot;</span><span class="p">,</span> <span class="s2">&quot;required&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">attr</span><span class="p">(</span><span class="s2">&quot;align_corners&quot;</span><span class="p">,</span> <span class="s2">&quot;required&quot;</span><span class="p">,</span> <span class="s2">&quot;bool&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">attr</span><span class="p">(</span><span class="s2">&quot;cust_aicpu&quot;</span><span class="p">,</span> <span class="s2">&quot;optional&quot;</span><span class="p">,</span> <span class="s2">&quot;str&quot;</span><span class="p">,</span> <span class="s2">&quot;aicpu_kernels&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">dtype_format</span><span class="p">(</span><span class="n">DataType</span><span class="o">.</span><span class="n">F32_Default</span><span class="p">,</span> <span class="n">DataType</span><span class="o">.</span><span class="n">F32_Default</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">dtype_format</span><span class="p">(</span><span class="n">DataType</span><span class="o">.</span><span class="n">F16_Default</span><span class="p">,</span> <span class="n">DataType</span><span class="o">.</span><span class="n">F32_Default</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">target</span><span class="p">(</span><span class="s2">&quot;Ascend&quot;</span><span class="p">)</span> \
<span class="gp">... </span>    <span class="o">.</span><span class="n">get_op_info</span><span class="p">()</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@custom_info_register</span><span class="p">(</span><span class="n">resize_bilinear_op_info</span><span class="p">)</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">resize_bilinear_aicpu</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">return</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">AicpuNet</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">AicpuNet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">resize_bilinear_op</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">Custom</span><span class="p">(</span><span class="n">resize_bilinear_aicpu</span><span class="p">,</span> <span class="n">out_shape</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> \
<span class="gp">... </span>                                             <span class="n">out_dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">func_type</span><span class="o">=</span><span class="s2">&quot;aicpu&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">resize_bilinear_op</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="s2">&quot;aicpu_kernels&quot;</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">res</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Example, func_type = &quot;aot&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">AOTSingleOutputNet</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">out_shapes</span><span class="p">,</span> <span class="n">out_types</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">AOTSingleOutputNet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">program</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">Custom</span><span class="p">(</span><span class="s2">&quot;./reorganize.so:CustomReorganize&quot;</span><span class="p">,</span> <span class="n">out_shapes</span><span class="p">,</span> <span class="n">out_types</span><span class="p">,</span> <span class="s2">&quot;aot&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">program</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Example, func_type = &quot;pyfunc&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">func_multi_output</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="p">(</span><span class="n">x1</span> <span class="o">+</span> <span class="n">x2</span><span class="p">),</span> <span class="p">(</span><span class="n">x1</span> <span class="o">-</span> <span class="n">x2</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">PyFuncNet</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">PyFuncNet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">Custom</span><span class="p">(</span><span class="n">func_multi_output</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">_</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">),</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">_</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">),</span> <span class="s2">&quot;pyfunc&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.ops.Assign.html" class="btn btn-neutral float-right" title="mindspore.ops.Assign" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.ops.SparseTensorDenseMatmul.html" class="btn btn-neutral float-left" title="mindspore.ops.SparseTensorDenseMatmul" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>